//
//  YMHomeRunOnView.swift
//  NewVersionDemo
//
//  Created by zeng on 2018/3/13.
//  Copyright © 2018年 youmy. All rights reserved.
//  疯狂抢购

import UIKit

fileprivate let kHomeRunonTopCellId = "kHomeRunonTopCellId"
fileprivate let kHomeRunonBottomCellId = "kHomeRunonBottomCellId"

fileprivate let kItemMargin:CGFloat = 1
fileprivate let kItemW:CGFloat = (UIScreen.main.bounds.width - 1 * kItemMargin) / 2
class YMHomeRunOnView: UIView {

    @IBOutlet weak var collectionView: UICollectionView!
    /// 抢购结束提示框
    @IBOutlet weak var overView: UIView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        autoresizingMask = UIViewAutoresizing()
        
        self.frame = CGRect(x: 0, y: 47, width: UIScreen.main.bounds.width, height: 250)
        
        collectionView.dataSource = self
        collectionView.delegate = self
        
        collectionView.register(UINib.init(nibName: "YMHomeRunOnTopCell", bundle: nil), forCellWithReuseIdentifier: kHomeRunonTopCellId)
        
        collectionView.register(UINib.init(nibName: "YMHomeRunOnBottomCell", bundle: nil), forCellWithReuseIdentifier: kHomeRunonBottomCellId)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.minimumLineSpacing = kItemMargin
        layout.minimumInteritemSpacing = kItemMargin
        layout.sectionInset = UIEdgeInsetsMake(kItemMargin, 0, 0, 0)
    }
}

extension YMHomeRunOnView:UICollectionViewDelegateFlowLayout{
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        if indexPath.section == 0 {
            let width = (UIScreen.main.bounds.width - kItemMargin) / 2
            return CGSize(width: width, height: 92)
        }else{
            let width = (UIScreen.main.bounds.width - 2 * kItemMargin) / 3
            return CGSize(width: width, height: 155)
        }
    }
}


// MARK: - UICollectionViewDataSource,UICollectionViewDelegate
extension YMHomeRunOnView:UICollectionViewDataSource,UICollectionViewDelegate{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 1 {
            return 3
        }
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if indexPath.section == 0 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kHomeRunonTopCellId, for: indexPath) as! YMHomeRunOnTopCell
            return cell
        }else{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kHomeRunonBottomCellId, for: indexPath) as! YMHomeRunOnBottomCell
            return cell
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("点击了抢购：\(indexPath.section == 0 ? "top-" : "bottom-")\(indexPath.item)")
    }
}



extension YMHomeRunOnView {
    class func nibView()-> YMHomeRunOnView{
        return Bundle.main.loadNibNamed("YMHomeRunOnView", owner: nil, options: nil)?.first as! YMHomeRunOnView
    }
}
